import router from '@/router';
export default {
    namespaced: true,
    state: () => ({
        path: '',
        editableTabsValue: 'home',
        editableTabs: [{
            name: 'home'
        }]
    }),
    mutations: {
        addTabs(state, val) {
            const arr = state.editableTabs.some(el => {
                return el.name == val.name
            });
            if (!arr) {
                state.editableTabs.push({
                    name: val.name,
                    closable: true
                })
            }
            state.editableTabs = state.editableTabs
            state.path = val.name
            state.editableTabsValue = val.name
        },
        deleteTabs(state, value) {
            let tabs = state.editableTabs;
            let activeName = state.path;
            if (activeName === value) {
                tabs.forEach((tab, index) => {
                    if (tab.name === value) {
                        let nextTab = tabs[index + 1] || tabs[index - 1];
                        if (nextTab) {
                            activeName = nextTab.name;
                            router.push(`/${activeName}`)
                        }
                    }
                });
            }
            const arr = tabs.filter(tab => tab.name !== value)
            state.editableTabs = arr
        },
        removeAllTabs(state) {
            state.editableTabs = []
        }
    },
    actions: {
        addTableTabs({ commit }, value) {
            commit('addTabs', value)
        }
    }
}